#!/usr/bin/python
import random
import pygtk
pygtk.require('2.0')
import gtk

def is_str(n):
    return type(n) == type(str())

def is_int(n):
    return type(n) == type(int())

def is_float(n):
    return type(n) == type(float())

def is_list(n):
    return type(n) == type(list())

def make_string(ls):
    s = []
    for c in ls:
        s.append(str(c))
    return ''.join(s)

def evaluate_input(code_word, difficulty):
    try:
        STDIN = raw_input("Please enter a combination: ")
        if(STDIN.lower() == "quit()" or STDIN.lower() == "q()"):
            print "Code word was:",make_string(code_word)
            exit()
        guess = map(int,list(STDIN))
        assert(len(guess) == difficulty)
        copy = code_word[:]
        correct_places = 0
        for i in range(0,len(guess)):
            if(guess[i] == code_word[i]):
                correct_places += 1
            if(guess[i] in copy):
                copy.remove(guess[i])
        return (correct_places, difficulty-len(copy), guess)             
    except ValueError:
        print "Could not read  input."
        return (-1,-1,-1)
    except AssertionError:
        print "Code word not right length."
        print "Enter a", difficulty, "digit combination."
        return -1,-1,-1

def play_game():
    print "\n"*50
    print "======================================================================"
    print "\n"
    print "Welcome to Master Mind!"
    print "You have 10 chances to guess the secret code word!"
    print "\n"
    print "======================================================================"
    difficulty = None
    while(not is_int(difficulty)):
        try:    
            print "\n"
            print "Choose how difficult you want the game to be!"
            print "The number you enter will be how long the code word's length."
            print "\n"
            difficulty = (raw_input("Please enter a difficulty: "))
            difficulty = int(difficulty)
            assert(difficulty > 0)
        except ValueError:
            if(str(difficulty).lower() == "q()" or str(difficulty).lower() =="quit()"):
                exit()
            else:
                difficulty = None
                print "Please enter an integer value."
        except AssertionError:
            print "Don't be silly, you can't have a difficulty of",difficulty
	    print "==============================================================="
            difficulty = None
    print "======================================================================"
    print "\n"
    num_guesses = 10
    code_word = list()
    for i in range(0,difficulty):
        code_word.append(random.randrange(0,10))

    while(num_guesses > 0):
        num_guesses -= 1 
        (correct_places, correct_nums) = (-1,-1)
        while(correct_places < 0):
            (correct_places,correct_nums,guess) = evaluate_input(code_word, difficulty)
        if(correct_places < difficulty):
            print "\n"
            print "Feedback:",make_string(guess)
            print "=================================================================="
            print "Numbers in the correct place:", correct_places
            print "Numbers correctly guessed:",correct_nums
            print "Guesses remaining:",num_guesses
            print "=================================================================="
        elif(correct_places == difficulty):
            print "Congratulations!"
            print "The code word was:",make_string(code_word)
            break
        else:
            print "This shouldn't ever happen."
        if(num_guesses <= 0):
            print "You've run out of guesses."
            print "The code word was:", make_string(code_word)
            break


def main():
    stop = False
    while(not stop):
        play_game()
        STDIN = False
        while(not is_str(STDIN)):
            try:
                STDIN = str(raw_input("Play again?[y/n] ")).lower()
                assert(STDIN == "yes" or STDIN == "y" or STDIN == "no" or STDIN == "n")
                if(STDIN == "yes" or STDIN == "y"):
                    stop = False
                elif(STDIN == "no" or STDIN == "n"):
                    stop = True
                else:
                    print "Can't read input. Please enter yes or no."
                    STDIN = False
            except AssertionError:
                print "Can't read input. Please enter yes or no."
                STDIN = False
        
            
if __name__ == "__main__":
    main()                
        
        
    
    



    
